package android.widget;

import com.example.adapter.SelectPopupListAdapter;
import com.example.smart_home.R;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;

public class SelectPopupDialog
  implements DialogInterface
{
  private static SelectPopupDialog sWacaiDialog;
  private Context mContext;
  private DialogInterface.OnClickListener mListener;
  private View mParenView;
  private PopupWindow mPopupWindow;

  public SelectPopupDialog(Context paramContext, View paramView, String[] paramArrayOfString, DialogInterface.OnClickListener paramOnClickListener)
  {
    this.mContext = paramContext;
    this.mListener = paramOnClickListener;
    this.mParenView = paramView;
    final View localView = ((LayoutInflater)paramContext.getSystemService("layout_inflater")).inflate(R.layout.select_popup_list, null);
    localView.setFocusable(true);
    localView.setFocusableInTouchMode(true);
    localView.setOnKeyListener(new View.OnKeyListener()
    {
      public boolean onKey(View paramAnonymousView, int paramAnonymousInt, KeyEvent paramAnonymousKeyEvent)
      {
        if (paramAnonymousInt == 4)
        {
          SelectPopupDialog.this.dismiss();
          return true;
        }
        return false;
      }
    });
    ListView localListView = (ListView)localView.findViewById(R.id.itemList);
    localListView.setAdapter(new SelectPopupListAdapter(paramContext, R.layout.list_select_item_pop, paramArrayOfString));
    localListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
      public void onItemClick(AdapterView<?> paramAnonymousAdapterView, View paramAnonymousView, int paramAnonymousInt, long paramAnonymousLong)
      {
        if (SelectPopupDialog.this.mListener != null)
          SelectPopupDialog.this.mListener.onClick(SelectPopupDialog.this, paramAnonymousInt);
        SelectPopupDialog.this.dismiss();
      }
    });
    this.mPopupWindow = new PopupWindow(localView, -2, -2);
    this.mPopupWindow.setOutsideTouchable(true);
    this.mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
    this.mPopupWindow.setFocusable(true);
    this.mPopupWindow.setTouchInterceptor(new View.OnTouchListener()
    {
      public boolean onTouch(View paramAnonymousView, MotionEvent paramAnonymousMotionEvent)
      {
        return localView.onTouchEvent(paramAnonymousMotionEvent);
      }
    });
  }

  public static boolean hide()
  {
    if (sWacaiDialog != null)
    {
      sWacaiDialog.dismiss();
      return true;
    }
    return false;
  }

  public static void show(Context paramContext, View paramView, int paramInt, DialogInterface.OnClickListener paramOnClickListener)
  {
    show(paramContext, paramView, paramContext.getResources().getStringArray(paramInt), paramOnClickListener);
  }

  public static void show(Context paramContext, View paramView, String[] paramArrayOfString, DialogInterface.OnClickListener paramOnClickListener)
  {
    sWacaiDialog = new SelectPopupDialog(paramContext, paramView, paramArrayOfString, paramOnClickListener);
    sWacaiDialog.show();
  }

  public void cancel()
  {
    dismiss();
  }

  public void dismiss()
  {
    if (this.mPopupWindow != null)
      this.mPopupWindow.dismiss();
  }

  public void show()
  {
    if (this.mParenView != null)
      this.mPopupWindow.showAsDropDown(this.mParenView);
    View localView=null;
   if(localView == null)
      localView = ((Activity)this.mContext).getWindow().peekDecorView();
    
    this.mPopupWindow.showAtLocation(localView, 17, 0, 0);
  }
}
